package jp.kiroru_inc.rs_hello_world;

import android.content.Context;
import android.renderscript.RSTextureView;
import android.renderscript.RenderScriptGL;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * @author Selphy
 */
public class MainTextureView extends RSTextureView {
	private HelloWorldScript script;
	private RenderScriptGL rs;

	public MainTextureView(Context context, AttributeSet attrs) {
		super(context, attrs);
		if (rs == null) {
			RenderScriptGL.SurfaceConfig config = new RenderScriptGL.SurfaceConfig();
			rs = createRenderScriptGL(config);
		}
		if (script == null)
			script = new HelloWorldScript(rs, context.getApplicationContext(), getResources());
	}

	@Override
	protected void onDetachedFromWindow() {
		script = null;
		if (rs != null) {
			rs = null;
			destroyRenderScriptGL();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		script.touchEvent(event.getX(), event.getY());
		return true;
	}
}